package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @since 2022/08/25 21:11
 * @author sunxuJ
 */
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderConverter orderConverter;
    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        Long userId = request.getUserId();
        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("userId",userId);
        List<Order> orders = orderMapper.selectByExample(example);
       // PageHelper.startPage(request.getPage(),request.getSize());

        List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();


        for (Order order : orders) {
            String orderId = order.getOrderId();
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            List<OrderItemDto> itemDtos = orderConverter.item2dto(orderItems);
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            orderDetailInfo.setOrderItemDto(itemDtos);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);
            orderDetailInfos.add(orderDetailInfo);
        }

       // PageInfo<OrderDetailInfo> orderDetailInfoPageInfo = new PageInfo<>(orderDetailInfos);
        @NotNull @Min(1) Integer page = request.getPage();
        @NotNull @Min(1) Integer size = request.getSize();
        List<OrderDetailInfo> subList = null;
        if(page*size>orders.size()){
            subList = orderDetailInfos.subList((page - 1) * size, orders.size());
        }else {
            subList = orderDetailInfos.subList((page - 1) * size, page * size);
        }

        OrderListResponse orderListResponse = new OrderListResponse();
        orderListResponse.setData(subList);
        orderListResponse.setTotal((long)orders.size());
       // orderListResponse.setData(orderDetailInfoPageInfo.getList());
       // orderListResponse.setTotal(orderDetailInfoPageInfo.getTotal());
        return orderListResponse;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {

        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        orderDetailResponse.setGoodsList(orderItemDtos);

        return orderDetailResponse;
    }
}
